
package SysUtils;

import scalaExec.Interpreter.GlobalValues;
import  java.awt.*;
import  java.awt.event.*;
import  javax.swing.*;
import  java.io.*;
import javax.swing.text.DefaultEditorKit;

 
public class ConsoleWindow {
    public JFrame consoleFrame = new JFrame();
    JPopupMenu  consolePopup = new JPopupMenu();
    JMenuItem cutJMenuItem = new JMenuItem(new DefaultEditorKit.CutAction());
    JMenuItem copyJMenuItem = new JMenuItem(new DefaultEditorKit.CopyAction());
    JMenuItem pasteJMenuItem = new JMenuItem(new DefaultEditorKit.PasteAction());
    JMenuItem verboseJMenuItem = new JMenuItem(new verboseOutputAction());
    public JTextArea  output = new JTextArea();
    public JScrollPane outputPane = new JScrollPane(output);
    JMenuItem clearOutputItem = new JMenuItem(new clearOutputAction(output));
        
    public static PrintStream consoleStream;

        public ConsoleWindow()
	{
           
      outputPane.setAutoscrolls(true);
      consolePopup.add(pasteJMenuItem);
      consolePopup.add(copyJMenuItem);
      consolePopup.add(cutJMenuItem);
      consolePopup.add(clearOutputItem);
      consolePopup.add(verboseJMenuItem);

     clearOutputItem.setAccelerator(KeyStroke.getKeyStroke("ctrl C"));

      output.add(consolePopup);
      output.setToolTipText("Displays Java's output sent at System.out stream and messages produced from Java's runtime exceptions");

      output.addMouseListener(new MouseAdapterForConsole());   // handles right mouse clicks

      consoleFrame.setTitle("scalaLab Console Standard Output and Error Window");

      int locX = 0; // loc.x;
      int locY = 0; //loc.y;

         Font consoleFont = new Font("Arial", Font.PLAIN, 16);
         output.setFont(consoleFont);
         output.setEditable(true);
         output.setToolTipText("Displays the program output redirecting the System.out.print(), System.err.print(), System.out.println() etc commands");
         JScrollPane outputScrollPane = new JScrollPane(output);

         consoleFrame.add(outputScrollPane);
         consoleFrame.setLocation(locX, locY);
         consoleFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);  // prevent closing the Console
         consoleFrame.setVisible(true);

         // define a PrintStream that sends its bytes to the output text area
         consoleStream = new PrintStream( new OutputStream () {
            @Override
                public void write(int b)  {}   // never called
                public void write(  byte []  b, int off, int len )
                {
                    String outStr = new String(b, off, len);
                    output.append(outStr);
                }
            });

            // set both System.out and System.err to that stream
        System.setOut(consoleStream);
        System.setErr(consoleStream);
      
 adjustScalaLabWindow();                
     }

public   void adjustScalaLabWindow() {
        Dimension ScreenDim = Toolkit.getDefaultToolkit().getScreenSize();
        
        double mainFrameRelSize = GlobalValues.mainFrameRelativeSize;
        int sizX = ScreenDim.width;
        int sizY = ScreenDim.height;
        if (GlobalValues.scalalabMainFrame.ySizeMainFrame > (int)(mainFrameRelSize*(double)sizY))  {
            GlobalValues.scalalabMainFrame.ySizeMainFrame = (int)(mainFrameRelSize*(double)sizY);
      GlobalValues.scalalabMainFrame.setSize(GlobalValues.scalalabMainFrame.xSizeMainFrame, GlobalValues.scalalabMainFrame.ySizeMainFrame);
      GlobalValues.scalalabMainFrame.setLocation(0, 0);
      consoleFrame.setSize(GlobalValues.scalalabMainFrame.xSizeMainFrame,  sizY - (int)(1.1*GlobalValues.scalalabMainFrame.ySizeMainFrame));
      consoleFrame.setLocation(GlobalValues.scalalabMainFrame.xLocMainFrame,
              GlobalValues.scalalabMainFrame.yLocMainFrame+(int)(1.02*GlobalValues.scalalabMainFrame.ySizeMainFrame));
      
        } 
        else {
      consoleFrame.setSize(GlobalValues.scalalabMainFrame.xSizeMainFrame,  sizY - (int)(1.1*GlobalValues.scalalabMainFrame.ySizeMainFrame));
      consoleFrame.setLocation(GlobalValues.scalalabMainFrame.xLocMainFrame,
              GlobalValues.scalalabMainFrame.yLocMainFrame+(int)(1.02*GlobalValues.scalalabMainFrame.ySizeMainFrame));
            
        }
 }

public   void readjustScalaLabWindow() {
        Dimension ScreenDim = Toolkit.getDefaultToolkit().getScreenSize();
        
        double mainFrameRelSize = GlobalValues.mainFrameRelativeSize;
        int sizX = ScreenDim.width;
        int sizY = ScreenDim.height;
      GlobalValues.scalalabMainFrame.ySizeMainFrame = (int)(mainFrameRelSize*(double)sizY);
      GlobalValues.scalalabMainFrame.setSize(GlobalValues.scalalabMainFrame.xSizeMainFrame, GlobalValues.scalalabMainFrame.ySizeMainFrame);
      GlobalValues.scalalabMainFrame.setLocation(0, 0);
      consoleFrame.setSize(GlobalValues.scalalabMainFrame.xSizeMainFrame,  sizY - (int)(1.1*GlobalValues.scalalabMainFrame.ySizeMainFrame));
      consoleFrame.setLocation(GlobalValues.scalalabMainFrame.xLocMainFrame,
            GlobalValues.scalalabMainFrame.yLocMainFrame+(int)(1.02*GlobalValues.scalalabMainFrame.ySizeMainFrame));
      
 }

       private class MouseAdapterForConsole  extends  MouseAdapter {
          public void mousePressed(MouseEvent e) {
              if (e.isPopupTrigger()){
                consolePopup.show((Component) e.getSource(), e.getX(), e.getY());
             }
           }

        public void mouseReleased(MouseEvent e) {
           if (e.isPopupTrigger()){
                 consolePopup.show((Component) e.getSource(), e.getX(), e.getY());
             }

          }

        }

     

}